<?php
/**
 * jishupu-cms
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2021/11/11
 */

namespace App\Http\Requests;

/**
 * 单页模块表单请求验证
 * Class InfoPageRequest
 * @package App\Http\Requests
 */
class InfoPageRequest extends BaseRequest
{
    /**
     * 用户是否有权操作，true是，false否
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * 字段验证规则
     *
     * @return array
     */
    public function rules()
    {
        return [
            'info_category_id' => 'required|integer',
            'title' => 'required|min:2|max:100',
            'cover_image' => [
                'nullable',
                function ($attribute, $value, $fail) {
                    AttachmentRequest::validity(__('common.cover_image'), $value, $fail, 1);
                }
            ],
            'keywords' => 'nullable|max:500',
            'description' => 'nullable|max:200',
            'content' => 'nullable',
            'status' => 'required|boolean'
        ];
    }

    /**
     * 提示信息
     *
     * @return array
     */
    public function messages()
    {
        return [
        ];
    }

    /**
     * 定义场景
     *
     * @var \string[][]
     */
    public $scenes = [
        'update' => ['title', 'cover_image', 'keywords', 'description', 'content', 'status']
    ];

    /**
     * 验证错误的自定义属性
     *
     * @return array
     */
    public function attributes()
    {
        return [
            'cate_id' => __('infoPage.cate_id'),
            'cover_image' => __('common.cover_image'),
            'title' => __('common.title'),
            'keywords' => __('common.keywords'),
            'description' => __('common.description'),
            'content' => __('common.content'),
            'status' => __('common.status'),
            'created_at' => __('common.created_at'),
        ];
    }


    /**
     * 验证前
     *
     * @return void
     */
    protected function prepareForValidation()
    {
        if (isset($this->keywords)) $this->offsetSet('keywords', join(',', $this->keywords));
        if (!isset($this->cover_image) || empty($this->cover_image)) $this->offsetSet('cover_image', 0);
    }

}
